struct Retirement {
    year: u32,
    month: u32,
    retire_year: u32,
    retire_month: u32,
    delay: u32,
}

impl Retirement {
    fn new(time: &str, tp: &str) -> Self {
        let year = time.split("-").collect::<Vec<&str>>()[0].parse::<u32>().unwrap();
        let month = time.split("-").collect::<Vec<&str>>()[1].parse::<u32>().unwrap();
        let retire_year = if tp == "原法定退休年龄55周岁女职工" {
            year + 55
        } else if tp == "原法定退休年龄50周岁女职工" {
            year + 50
        } else if tp == "男职工" {
            year + 60
        } else {
            unimplemented!("未实现的退休类型")
        };
        Retirement {
            year,
            month,
            retire_year,
            retire_month: month,
            delay: 0,
        }
    }

    fn apply_policy(&mut self, policy: &Policy) {
        if self.retire_year < policy.start_year {
            return;
        }
        if self.retire_year <= policy.end_year - policy.total {
            let diff = (self.retire_year - policy.start_year) * 12 + self.retire_month - 1;
            self.delay = diff / policy.base + 1;
        } else {
            let diff = (policy.end_year - policy.start_year - policy.total + 1) * 12;
            self.delay = diff / policy.base;
        };
        self.retire_month += self.delay - 1;
        self.retire_year += self.retire_month / 12;
        self.retire_month = self.retire_month % 12 + 1;
    }
}

// 政策变化
struct Policy {
    start_year: u32,
    end_year: u32,
    base: u32,
    total: u32,
}

impl Policy {
    fn new(start_year: u32, end_year: u32, tp: &str) -> Self {
        let (base, total) = if tp == "原法定退休年龄55周岁女职工" {
            (4, 3)
        } else if tp == "原法定退休年龄50周岁女职工" {
            (2, 5)
        } else if tp == "男职工" {
            (4, 3)
        } else {
            unimplemented!("未实现的退休类型")
        };
        Policy {
            start_year,
            end_year,
            base,
            total,
        }
    }
}


pub fn retire_time(time: &str, tp: &str) -> String {
    // time : 1971-04
    // tp : 原法定退休年龄55周岁女职工
    // tp : 原法定退休年龄50周岁女职工
    // tp : 男职工
    // 返回值示例：2026-08,55.33,4
    let mut ret = Retirement::new(time,tp);
    let policy_2025 = Policy::new(2025, 2039, tp);
    ret.apply_policy(&policy_2025);
    let retire_time =  (ret.retire_year - ret.year) * 12 + ret.retire_month - ret.month;
    let retire_time = retire_time as f32 / 12.0;
    // 整数不输出小数点
    if retire_time == retire_time.floor() {
        return format!("{}-{:02},{:.0},{:.0}", ret.retire_year, ret.retire_month, retire_time, ret.delay)
    }
    format!("{}-{:02},{:.2},{:.0}", ret.retire_year, ret.retire_month, retire_time as f32, ret.delay)
}
